package UILayer;

import CtrLayer.CustomerCtr;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;


public class CustomerManager extends JInternalFrame
{

	private JPanel contentPane;
	private JPanel panel;
	private CustomerCtr customers;
	private JTextField name_tf;
	private JTextField id_tf;
	private JTextField phone_tf;
	private JTextField city_tf;
	private JTextField add_tf;
	private JTextField post_tf;
	private JComboBox comboBox;
	private JLabel[] empty_lbls;
	private JLabel inc_phone;
	private JLabel inc_postal;
	private JLabel no_group;
	private JButton groups;
	private String id;
	int view;
	
	public CustomerManager(String id, int view)
	{
		super("Add new Customer", true, true, true, true);
		this.view = view;
		if (view == 1)
		{
			this.setTitle("View customer");
		}
		else if (id != null)
			{
				this.setTitle("Edit customer");
			}
		this.id = id;
		customers = new CustomerCtr();

		setBounds(100, 100, 424, 412);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));

		JButton ok = new JButton("Ok");

		ok.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				create_edit();
			}
		});

		panel_1.add(ok, BorderLayout.WEST);

		JButton cancel = new JButton("Cancel");
		cancel.setHorizontalAlignment(SwingConstants.RIGHT);
		cancel.setForeground(Color.BLACK);
		cancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});
		cancel.setBackground(new Color(240, 240, 240));
		panel_1.add(cancel, BorderLayout.EAST);

		groups = new JButton("Manage Groups");
		groups.setBounds(202, 250, 107, 23);
		groups.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				GroupsUI frame = new GroupsUI();
				getDesktopPane().add(frame);
				frame.setVisible(true);
				dispose();
			}
		});
		groups.setVisible(false);
		panel.add(groups);

		make_lbls();
		
		make_empty_lbls();
		make_error_lbls();
		make_fields();
	}

	private void make_error_lbls()
	{
		inc_phone = new JLabel("<html>The phone number is incorrect.<br/>Please enter valid danish phone number.</html>");
		inc_phone.setForeground(Color.RED);
		inc_phone.setLabelFor(city_tf);
		inc_phone.setBounds(202, 86, 230, 42);
		inc_phone.setVisible(false);
		panel.add(inc_phone);

		inc_postal = new JLabel("Enter valid Danish postal code.");
		inc_postal.setForeground(Color.RED);
		inc_postal.setBounds(202, 160, 290, 14);
		inc_postal.setVisible(false);
		panel.add(inc_postal);

		no_group = new JLabel(
				"<html>There are no groups.<br/>Please create one before continuing.");
		no_group.setForeground(Color.RED);
		no_group.setBounds(202, 209, 186, 42);
		no_group.setVisible(false);
		panel.add(no_group);
	}

	private void make_lbls()
	{
		JLabel lblNewLabel = new JLabel("Customer data");
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
		lblNewLabel.setBounds(10, 11, 138, 14);
		panel.add(lblNewLabel);

		JLabel id_lbl = new JLabel("Client number");
		id_lbl.setBounds(10, 45, 67, 14);
		panel.add(id_lbl);

		JLabel name_lbl = new JLabel("Name");
		name_lbl.setBounds(10, 73, 46, 14);
		panel.add(name_lbl);

		JLabel phone_lbl = new JLabel("Phone Number");
		phone_lbl.setBounds(10, 101, 79, 14);
		panel.add(phone_lbl);

		JLabel add_lbl = new JLabel("Address");
		add_lbl.setBounds(10, 129, 86, 14);
		panel.add(add_lbl);

		JLabel city_lbl = new JLabel("City");
		city_lbl.setBounds(10, 191, 46, 14);
		panel.add(city_lbl);

		JLabel group_lbl = new JLabel("Group");
		group_lbl.setBounds(10, 222, 79, 14);
		panel.add(group_lbl);

		JLabel group_nr_lbl = new JLabel("Postal code");
		group_nr_lbl.setBounds(10, 160, 86, 14);
		panel.add(group_nr_lbl);
	}

	private void make_fields()
	{
		name_tf = new JTextField();
		name_tf.setBounds(113, 70, 86, 20);
		panel.add(name_tf);
		name_tf.setColumns(10);

		id_tf = new JTextField();
		id_tf.setText(customers.get_next_cust_nr());
		id_tf.setEnabled(false);
		id_tf.setEditable(false);
		id_tf.setBounds(113, 42, 86, 20);
		panel.add(id_tf);
		id_tf.setColumns(10);

		phone_tf = new JTextField();
		phone_tf.setBounds(113, 98, 86, 20);
		panel.add(phone_tf);
		phone_tf.setColumns(10);

		city_tf = new JTextField();
		city_tf.setBounds(113, 188, 86, 20);
		panel.add(city_tf);
		city_tf.setColumns(10);

		add_tf = new JTextField();
		add_tf.setBounds(113, 126, 86, 20);
		panel.add(add_tf);
		add_tf.setColumns(10);

		post_tf = new JTextField();
		post_tf.setBounds(113, 157, 86, 20);
		panel.add(post_tf);
		post_tf.setColumns(10);

		comboBox = new JComboBox();
		String[] temp = customers.all_groups();
		comboBox.setModel(new DefaultComboBoxModel(temp));
		comboBox.setBounds(113, 219, 86, 20);
		panel.add(comboBox);
		if (id != null)
		{
			fill_tfs();
			if (view == 1)
			{
				name_tf.setEnabled(false);
				id_tf.setEnabled(false);
				phone_tf.setEnabled(false);
				city_tf.setEnabled(false);
				add_tf.setEnabled(false);
				post_tf.setEnabled(false);
				comboBox.setEnabled(false);
			}
		}
		else
		{
			if (comboBox.getItemCount() == 0)
			{
				no_group.setVisible(true);
				groups.setVisible(true);

			}
		}
	}

	private void fill_tfs()
	{
		String[] data = customers.give_string_array(id);
		id_tf.setText(data[0]);
		name_tf.setText(data[1]);
		phone_tf.setText(data[2]);
		add_tf.setText(data[3]);
		post_tf.setText(data[4]);
		city_tf.setText(data[5]);
		comboBox.setSelectedItem(data[6]);
	}

	private void create_edit()
	{
		if (check()==true)
		{
			String name = name_tf.getText();
			String phone = phone_tf.getText();
			String category = (String) comboBox.getSelectedItem();
			String city = city_tf.getText();
			;
			String address = add_tf.getText();
			String postal = post_tf.getText();

			if (id == null)
			{
				if (customers.find_customer_by_name(name) == null)
				{
					customers.add_customer(name, phone, address, postal, city, category);
					JOptionPane.showMessageDialog(this, "Successully created a customer.", "Success", JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
				}
				else
				{
					JOptionPane.showMessageDialog(this,"Couldn't create a customer.\nClient with this name already exists.", "Error", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			else if (view == 0)
			{
				if (customers.find_customer_by_name(name) == null)
				{
					customers.update_customer(id, name, phone, address,
							postal, city, category);
					JOptionPane.showMessageDialog(this,
							"Successully edited a Customer's information.",
							"Success", JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
				}
				else
				{
					JOptionPane.showMessageDialog(this,
							"Couldn't edit a customer.", "Error",
							JOptionPane.INFORMATION_MESSAGE);
				}
			}
			else
			{
				this.dispose();
			}
		}
	}

	private boolean check()
	{
		inc_phone.setVisible(false);
		inc_postal.setVisible(false);
		no_group.setVisible(false);
		empty_lbls[0].setVisible(false);
		empty_lbls[1].setVisible(false);
		empty_lbls[2].setVisible(false);

		boolean[] is_lbl = { true, true };
		String phone = phone_tf.getText();
		String new_phone;
		if (phone.matches("\\+?[0-9]+(:?-[0-9]+)*"))
		{
			if (phone.contains("-"))
			{
				new_phone = phone.replaceAll("-", "");
			}
			else
			{
				new_phone = phone;
			}

			if ((new_phone.length() == 8)
					|| (new_phone.length() == 11 && new_phone.startsWith("+")))
			{
				is_lbl[0] = false;
			}
			else
			{
				is_lbl[0] = true;
			}
		}
		else
		{
			is_lbl[0] = true;
		}
		if (post_tf.getText().matches("[0-9]+")
				&& post_tf.getText().length() == 4)
		{
			is_lbl[1] = false;
		}
		else
		{
			is_lbl[1] = true;
		}

		return display_lbls(is_lbl);
	}

	private boolean display_lbls(boolean[] display_lbl)
	{
		boolean returner = true;
		if (name_tf.getText().equals(""))
		{
			empty_lbls[0].setVisible(true);
			returner = false;
		}
		if (add_tf.getText().equals(""))
		{
			empty_lbls[1].setVisible(true);
			returner = false;
		}
		if (city_tf.getText().equals(""))
		{
			empty_lbls[2].setVisible(true);
			returner = false;
		}
		if (phone_tf.getText().equals(""))
		{
			inc_phone.setVisible(true);
			returner = false;
		}
		if (post_tf.getText().equals(""))
		{
			inc_postal.setVisible(true);
			returner = false;
		}
		if (display_lbl[0])
		{
			inc_phone.setVisible(true);
			returner = false;
		}
		if (display_lbl[1])
		{
			inc_postal.setVisible(true);
			returner = false;
		}
		if (comboBox.getItemCount() == 0)
		{
			no_group.setVisible(true);
			returner = false;
		}

		return returner;
	}

	private void make_empty_lbls()
	{
		int[][] loc = { { 209, 73 }, { 209, 129 }, { 209, 185 } };
		empty_lbls = new JLabel[3];
		for (int i = 0; i <= 2; i++)
		{
			empty_lbls[i] = new JLabel("Fill.");
			empty_lbls[i].setForeground(Color.RED);
			empty_lbls[i].setBounds(loc[i][0], loc[i][1], 150, 14);
			empty_lbls[i].setVisible(false);
			panel.add(empty_lbls[i]);
		}

	}
}
